home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / shared_lib / funcs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-09  |  1.5 KB  |  82 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  FUNCS.C
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. /*
  14.  *  library interface
  15.  */
  16.  
  17. Prototype LibCall void LockTestLib(void);
  18. Prototype LibCall void UnLockTestLib(void);
  19. Prototype LibCall void PostString(__A0 const char *);
  20. Prototype LibCall long GetString(__A0 char *, __D0 long);
  21.  
  22. /*
  23.  *  library local
  24.  */
  25.  
  26. LibCall void
  27. LockTestLib(void)
  28. {
  29.     ObtainSemaphore(&SemLock);
  30. }
  31.  
  32. LibCall void
  33. UnLockTestLib(void)
  34. {
  35.     ReleaseSemaphore(&SemLock);
  36. }
  37.  
  38. LibCall void
  39. PostString(name)
  40. __A0 const char *name;
  41. {
  42.     Node *node;
  43.  
  44.     ObtainSemaphore(&SemLock);
  45.     node = AllocMem(sizeof(Node) + strlen(name) + 1, MEMF_PUBLIC);
  46.     node->ln_Name = (char *)(node + 1);
  47.     strcpy(node->ln_Name, name);
  48.     AddTail(&StrList, node);
  49.     ReleaseSemaphore(&SemLock);
  50. }
  51.  
  52. /*
  53.  *  returns actual length of returned string regardless of buffer size,
  54.  *  but only copies max chars to the buffer (including \0 which may
  55.  *  cut off part of the string if the string is too large to fit)
  56.  */
  57.  
  58. LibCall long
  59. GetString(buf, max)
  60. __A0 char *buf;
  61. __D0 long max;
  62. {
  63.     Node *node;
  64.     long len;
  65.  
  66.     ObtainSemaphore(&SemLock);
  67.     if (node = RemHead(&StrList)) {
  68.     len = strlen(node->ln_Name);
  69.     strncpy(buf, node->ln_Name, max);
  70.     if (len >= max)
  71.         buf[max-1] = 0;
  72.     FreeMem(node, sizeof(Node) + len + 1);
  73.     } else {
  74.     len = -1;
  75.     if (max > 0)
  76.         buf[0] = 0;
  77.     }
  78.     ReleaseSemaphore(&SemLock);
  79.     return (len);
  80. }
  81.  
  82.